package process;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jasypt.util.password.ConfigurablePasswordEncryptor;

import dao_manager.UserInfoManager;
import dao_manager.UserInfoManagerImpl;
import ExceptionPerso.DupplicatedEntry;
import beans.UserRights;
import beans.user_Info;

@WebServlet("/jump")
public class Jump extends HttpServlet {
    
    private static final String ATT_USER_NAME = "userName";
    private static final String ATT_USER_PRENOM = "userPrenom";
    private static final String ATT_USER_ID      = "userID";
    private String    vue         = "/pages/newClient.jsp";
    private static final String ALGO_CHIFFREMENT = "SHA-256";
    
    
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        try {
            doRequest( request, response );
        } catch ( DupplicatedEntry e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
        try {
            doRequest( request, response );
        } catch ( DupplicatedEntry e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doRequest( HttpServletRequest request, HttpServletResponse response ) throws ServletException,
            IOException, DupplicatedEntry {
        
        System.out.println( " ************* demarrage de la servlet jump *******  ") ; 
        
        user_Info testUser = new user_Info();
        testUser.setUserName( "toumi" );
        testUser.setUserPrenom( "mohamed" );
        
        UserRights testRights = new UserRights();
        
        ConfigurablePasswordEncryptor passwordEncryptor = new ConfigurablePasswordEncryptor();
        passwordEncryptor.setAlgorithm(ALGO_CHIFFREMENT);
        passwordEncryptor.setPlainDigest(false);
        testRights.setUserMdp( passwordEncryptor.encryptPassword("test") );
        testRights.setUserRightsIndex( 2 );
        
        testUser.setUserRights( testRights );
        UserInfoManager userInfoManager = new UserInfoManagerImpl();
        userInfoManager.saveNewUser( testUser  , testRights );
        
        request.getSession().setAttribute( ATT_USER_NAME, testUser.getUserName() );
        request.getSession().setAttribute( ATT_USER_PRENOM, testUser.getUserPrenom() );
        request.getSession().setAttribute( ATT_USER_ID, testUser.getId() );
      
         testUser = new user_Info();
        testUser.setUserName( "toumi3" );
        testUser.setUserPrenom( "mohamed2" );
        
         testRights = new UserRights();
        
         passwordEncryptor = new ConfigurablePasswordEncryptor();
        passwordEncryptor.setAlgorithm(ALGO_CHIFFREMENT);
        passwordEncryptor.setPlainDigest(false);
        testRights.setUserMdp( passwordEncryptor.encryptPassword("test3") );
        testRights.setUserRightsIndex( 3 );
        
        testUser.setUserRights( testRights );
        userInfoManager.saveNewUser( testUser  , testRights );        
        
        this.getServletContext().getRequestDispatcher( vue ).forward( request, response );
    }

}
